package com.projecthost.wizaz;

import java.util.List;

import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.projecthost.util.HtmlParser;

public class WizazCrawlerBase {

    protected final HtmlParser htmlParser;
    protected final String mainWizazUrl;

    public WizazCrawlerBase(HtmlParser htmlParser, String mainWizazUrl) {
        this.htmlParser = htmlParser;
        this.mainWizazUrl = mainWizazUrl;
    }

    protected float getStarsValue(HtmlElement starsElement) {
        int fullStars = starsElement.getElementsByAttribute("li", "class", "sfull").size();
        int halfStars = starsElement.getElementsByAttribute("li", "class", "shalf").size();
        float starsValue = (1 * fullStars) + (0.5f * halfStars);
        return starsValue;
    }

    protected String getNextPageUrl(HtmlPage currentPage) {
        @SuppressWarnings("unchecked")
        List<HtmlAnchor> paginationAHrefs = (List<HtmlAnchor>) currentPage.getByXPath("//ul[@class='pagination'][1]/li/a");
        HtmlAnchor lastLink = paginationAHrefs.get(paginationAHrefs.size() - 1);
        String nextPageUrl = null;
        if ("Następna".equals(lastLink.getTextContent())) {
            nextPageUrl = lastLink.getHrefAttribute();
        }
        return nextPageUrl;
    }

}
